package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemMergePhoneNumBinding;
import com.yikaiye.android.yikaiye.model.UserModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;

public class MergeAccountAdapter extends BaseQuickAdapter<UserModel.MergeTelModel, DataBindingHolder<ItemMergePhoneNumBinding>> {

    private final Context context;
    private OnMergeClickListener onMergeClickListener;

    public MergeAccountAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemMergePhoneNumBinding> itemPhoneNumBindingDataBindingHolder, int i, @Nullable UserModel.MergeTelModel model) {
        ItemMergePhoneNumBinding binding = itemPhoneNumBindingDataBindingHolder.getBinding();
        if (model != null) {
            binding.phoneTv.setText(CommonUtil.getEncryptPhone(model.getTelNumber()));
            binding.mergeTv.setText(model.getMappingStatus() == 1 ? context.getString(R.string.merge_success) : context.getString(R.string.merge));
            binding.mergeSuccessIv.setVisibility(model.getMappingStatus() == 1 ? View.VISIBLE : View.GONE);
            binding.mergeTv.setEnabled(model.getMappingStatus() == 0);
            binding.mergeTv.setOnClickListener(view -> onMergeClickListener.onMergeClickListener(model.getTelNumber(), model, i));
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemMergePhoneNumBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_merge_phone_num, viewGroup);
    }

    public interface OnMergeClickListener {
        void onMergeClickListener(String phone, UserModel.MergeTelModel model, int position);
    }

    public void setOnMergeClickListener(OnMergeClickListener onMergeClickListener) {
        this.onMergeClickListener = onMergeClickListener;
    }
}
